{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4421b9c8",
   "metadata": {},
   "source": [
    "-----\n",
    "* 本周主要内容：图像识别（人脸识别）\n",
    "* week04\n",
    "* 记录人：黄斐珍\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79347aa5",
   "metadata": {},
   "source": [
    "# 上周内容\n",
    "\n",
    "> 1. API文档阅读(考试)\n",
    "> 2. requests模块(考试)\n",
    "> 3. 百度通用图像识别\n",
    "\n",
    "# 本周内容\n",
    "\n",
    "## 1. 人脸识别概念和基础\n",
    "## 2. 人脸识别应用\n",
    "## 3. 人脸识别实践-face++\n",
    "> 1. 人脸检测\n",
    "> 2. 人脸比对+人脸搜索\n",
    ">> 1. 人脸集合创建\n",
    ">> 2. 人脸"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77c35ad9",
   "metadata": {},
   "source": [
    "# 人脸识别概念和基础\n",
    "\n",
    "[知智1分钟人脸识别](https://zhuanlan.zhihu.com/p/36262110)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c1580005",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1681115605,a43cfba9-13b2-45d0-9090-eea474c95609', 'time_used': 567, 'faces': [{'face_token': 'cbc391308db69beee4068fe112af78a9', 'face_rectangle': {'top': 582, 'left': 340, 'width': 673, 'height': 673}, 'attributes': {'gender': {'value': 'Male'}, 'age': {'value': 22}, 'smile': {'value': 0.017, 'threshold': 50.0}, 'headpose': {'pitch_angle': 15.561225, 'roll_angle': 14.335487, 'yaw_angle': -13.872031}, 'blur': {'blurness': {'value': 0.377, 'threshold': 50.0}, 'motionblur': {'value': 0.377, 'threshold': 50.0}, 'gaussianblur': {'value': 0.377, 'threshold': 50.0}}, 'emotion': {'anger': 0.014, 'disgust': 0.003, 'fear': 0.006, 'happiness': 0.014, 'neutral': 99.605, 'sadness': 0.357, 'surprise': 0.003}, 'facequality': {'value': 45.915, 'threshold': 70.1}, 'beauty': {'male_score': 84.522, 'female_score': 89.999}}}], 'image_id': 'JCtR+/0G5deze9yfrHaHHQ==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸检测\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('cxk.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload = {\n",
    "    'api_key':'jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov',\n",
    "    'api_secret':'Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3',\n",
    "    'image_base64':img,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,emotion,blur,beauty'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data = payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d73173a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1681115607,660ca7f3-19c0-44bf-8086-54f38642d049', 'time_used': 631, 'faces': [{'face_token': '259cfe9128d53128dc475501ed8018b2', 'face_rectangle': {'top': 251, 'left': 349, 'width': 234, 'height': 234}, 'attributes': {'gender': {'value': 'Female'}, 'age': {'value': 26}, 'smile': {'value': 0.014, 'threshold': 50.0}, 'headpose': {'pitch_angle': 3.7136126, 'roll_angle': -12.703638, 'yaw_angle': 29.853497}, 'blur': {'blurness': {'value': 0.034, 'threshold': 50.0}, 'motionblur': {'value': 0.034, 'threshold': 50.0}, 'gaussianblur': {'value': 0.034, 'threshold': 50.0}}, 'emotion': {'anger': 0.185, 'disgust': 0.131, 'fear': 0.02, 'happiness': 93.17, 'neutral': 6.193, 'sadness': 0.28, 'surprise': 0.02}, 'facequality': {'value': 0.006, 'threshold': 70.1}, 'beauty': {'male_score': 74.338, 'female_score': 75.584}}}], 'image_id': 'zhDEn1Gqqn6tBWUd7+ivPw==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸检测\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('cyx.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload = {\n",
    "    'api_key':'KrYzgpdeZTU1mL6LsWC2yQD9_3dXKuSC',\n",
    "    'api_secret':'KU72UW6Tp6W60ZocPrlPKTgqSWLLCpKY',\n",
    "    'image_base64':img,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,emotion,blur,beauty'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data = payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "258.237px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
